<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="生产地" prop="producerId">
        <el-select v-model="queryParams.producerId" clearable placeholder="请选择">
          <el-option v-for="item in producerList" :key="item.producerId" :label="item.producerName"
            :value="item.producerId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" clearable placeholder="请选择">
          <el-option v-for="item in supplierList" :key="item.supplierId" :label="item.supplierName"
            :value="item.supplierId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="一级分类" prop="goodsTypeLevel1Id">
        <el-select @change="changeTypeOne()" v-model="queryParams.goodsTypeLevel1Id" clearable placeholder="请选择">
          <el-option v-for="item in typeListOne" :key="item.goodsTypeId" :label="item.goodsTypeName"
            :value="item.goodsTypeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类" prop="goodsTypeLevel2Id">
        <el-select @change="changeTypeTwo()" v-model="queryParams.goodsTypeLevel2Id" clearable placeholder="请选择">
          <el-option v-for="item in typeListTwo" :key="item.goodsTypeId" :label="item.goodsTypeName"
            :value="item.goodsTypeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类" prop="goodsTypeLevel3Id">
        <el-select v-model="queryParams.goodsTypeLevel3Id" clearable placeholder="请选择">
          <el-option v-for="item in typeListThr" :key="item.goodsTypeId" :label="item.goodsTypeName"
            :value="item.goodsTypeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['goods:goods:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['goods:goods:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['goods:goods:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['goods:goods:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编号" align="center" prop="goodsId" />
      <el-table-column label="商品编码" align="center" prop="goodsCode" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="生产厂商" align="center" prop="producerName" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="商品单位" align="center" prop="unitName" />
      <el-table-column label="一级类别" align="center" prop="goodsTypeNameOne" />
      <el-table-column label="二级类别" align="center" prop="goodsTypeNameTwo" />
      <el-table-column label="三级类别" align="center" prop="goodsTypeNameThr" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['goods:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商品信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品编码" prop="goodsCode">
          <el-input v-model="form.goodsCode" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>

        <el-form-item label="单位" prop="unitId">
          <el-select v-model="form.unitId" clearable placeholder="请选择">
            <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName"
              :value="item.unitId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="生产地" prop="producerId">
          <el-select v-model="form.producerId" clearable placeholder="请选择">
            <el-option v-for="item in producerList" :key="item.producerId" :label="item.producerName"
              :value="item.producerId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="供应商" prop="supplierId">
          <el-select v-model="form.supplierId" clearable placeholder="请选择">
            <el-option v-for="item in supplierList" :key="item.supplierId" :label="item.supplierName"
              :value="item.supplierId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="一级分类" prop="goodsTypeId">
          <el-select @change="changeTypeOneForm()" v-model="form.goodsTypeLevel1Id" clearable placeholder="请选择">
            <el-option v-for="item in typeListOne" :key="item.goodsTypeId" :label="item.goodsTypeName"
              :value="item.goodsTypeId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" prop="goodsTypeId">
          <el-select @change="changeTypeTwoForm()" v-model="form.goodsTypeLevel2Id" clearable placeholder="请选择">
            <el-option v-for="item in typeListTwo" :key="item.goodsTypeId" :label="item.goodsTypeName"
              :value="item.goodsTypeId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="三级分类" prop="goodsTypeId">
          <el-select v-model="form.goodsTypeLevel3Id" clearable placeholder="请选择">
            <el-option v-for="item in typeListThr" :key="item.goodsTypeId" :label="item.goodsTypeName"
              :value="item.goodsTypeId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/goods/goods";
import { listProducer } from "@/api/goods/producer";
import { suplierList } from '@/api/goods/supplier';
import { listByPid } from '@/api/goods/type';
import { allListUnit } from '@/api/goods/unit';
export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息管理表格数据
      goodsList: [],
      //生产地下拉列表数据
      producerList: [],
      //供应商下拉列表数据
      supplierList: [],
      //一级分类下拉列表数据
      typeListOne: [],
      //二级分类下拉列表数据
      typeListTwo: [],
      //三级分类下拉列表数据
      typeListThr: [],
      //商品单位下拉列表数据
      unitList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        producerId: null,
        supplierId: null,
        goodsTypeLevel1Id: null,
        goodsTypeLevel2Id: null,
        goodsTypeLevel3Id: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getProducerList();
    this.getSuplierList();
    this.getListByPid(0, 1);
    this.getAllUnitList();
  },
  methods: {
    changeTypeTwoForm() {
      this.getListByPid(this.form.goodsTypeLevel2Id, 3);
    },
    changeTypeOneForm() {
      this.getListByPid(this.form.goodsTypeLevel1Id, 2);
    },
    getAllUnitList(){
      allListUnit().then(response=>{
        this.unitList = response.data;
      });
    },
    changeTypeTwo() {
      this.getListByPid(this.queryParams.goodsTypeLevel2Id, 3);
    },
    changeTypeOne() {
      this.getListByPid(this.queryParams.goodsTypeLevel1Id, 2);
    },
    getListByPid(pid, level) {
      console.log(pid);
      // if(pid){return;}
      listByPid(pid).then(response => {
        if (level == 1) {
          this.typeListOne = response.data;
        } else if (level == 2) {
          this.typeListTwo = response.data;
        } else if (level == 3) {
          this.typeListThr = response.data;
        }
      })
    },
    /**查询所有的供应商信息列表 */
    getSuplierList() {
      suplierList().then(response => {
        this.supplierList = response.rows;
      })
    },

    /** 查询生产场地信息列表 */
    getProducerList() {
      listProducer(null).then(response => {
        this.producerList = response.rows;
      })
    },
    /** 查询商品信息管理列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        goodsCode: null,
        goodsName: null,
        producerId: null,
        supplierId: null,
        unitId: null,
        goodsTypeLevel1Id: null,
        goodsTypeLevel2Id: null,
        goodsTypeLevel3Id: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids
      getGoods(goodsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$modal.confirm('是否确认删除商品信息管理编号为"' + goodsIds + '"的数据项？').then(function () {
        return delGoods(goodsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goods/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
